package com.os.osframe.scaffold.xml;

import com.os.osframe.scaffold.domain.ScaffoldXmlTables;
import com.os.osframe.scaffold.util.JaxbUtil;
import com.os.osframe.scaffold.util.PropertyConfigurerLoader;
import com.os.osframe.scaffold.util.SpringUtils;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 将xml转为对象
 * Created by wangdc on 2015-4-26.
 */
public class ReadXmlObj {
    protected static PropertyConfigurerLoader propertyConfigurer;
    protected static ReadXmlObj readXmlObj;
    public static PropertyConfigurerLoader getPropertyConfigurer(){
        if(propertyConfigurer==null){
            propertyConfigurer= (PropertyConfigurerLoader)SpringUtils.getBean("propertyConfigurer");
        }
        return propertyConfigurer;
    }
    protected static Document document;


    protected static Map xmlObjMap;//读取的xml信息

    public static List getXmlObjList(String packages) throws Exception{
        ScaffoldXmlTables tables= getXmlObj(packages);
        if(tables==null){
            return null;
        }
        return tables.getScaffoldXmlTableList();
    }

    public static ScaffoldXmlTables getXmlObj(String packages) throws Exception{
        if(xmlObjMap==null){
            xmlObjMap=new HashMap();
        }
        if(xmlObjMap.containsKey(packages)){
            return (ScaffoldXmlTables)xmlObjMap.get(packages);
        }
        ScaffoldXmlTables tables= getDataInfoByModule(packages);
        xmlObjMap.put(packages,tables);
        return tables;
    }


    /**
     * 获取xml对象列表
     * @param packages
     * @return
     * @throws Exception
     */
    public static ScaffoldXmlTables getDataInfoByModule(String packages)throws Exception{
        String basePath = (String) getPropertyConfigurer().getProperty("osframe.file.path");//获取属性配置的服务器路径
        if (basePath.substring(basePath.length() - 1).equals("/")) {
            basePath = basePath.substring(basePath.length() - 1);
        }
        //String path = ScaffoldUtil.replaceStr(new File("").getAbsolutePath(), "\\", "/");//获取项目的根路径，这里获取到的是softdp的路径
        String targetFilePath=basePath;
        targetFilePath+="/auto/config/tables/"+packages+".xml";
        //List list=getDataInfo(targetFilePath);
        ScaffoldXmlTables scaffoldXmlTables=fileToBean(targetFilePath);
        return scaffoldXmlTables;
    }

    /**
     * 根据文件夹获取
     * @param folder
     * @return
     * @throws Exception
     */
    public static List getDataInfo(String folder)throws Exception{
        List list=new ArrayList();
        File folderFile = new File(folder);
        if (folderFile.isDirectory()) {//当是文件夹时，获取列表出来遍历递归
            String[] files = folderFile.list();
            if (files != null) {
                for (int i = 0; i < files.length; i++) {
                    if (files[i].startsWith(".") || !"xml".equals(files[i].substring(files[i].lastIndexOf(".")+1)))
                        continue;
                    ScaffoldXmlTables scaffoldXmlTables=fileToBean(folder+"/"+files[i]);
                    if(scaffoldXmlTables!=null){
                        list.addAll(scaffoldXmlTables.getScaffoldXmlTableList());
                    }
                }
            }
        }
        return list;
    }

    /**
     * 将xml文件转为对象
     * @param filePath
     * @return
     * @throws Exception
     */
    public static ScaffoldXmlTables fileToBean(String filePath)throws Exception{
        if(filePath==null || "".equals(filePath)){
            return null;
        }
        document=null;
        File file = new File(filePath);
        SAXReader saxReader = new SAXReader();// 用来读取xml文档
        document = saxReader.read(file);// 读取xml文档
        String xml=document.asXML();
        System.out.println(xml);
        if(xml==null || "".equals(xml)){
            return null;
        }

        ScaffoldXmlTables scaffoldXmlTables= JaxbUtil.converyToJavaBean(xml, ScaffoldXmlTables.class);
         return scaffoldXmlTables;
    }

}
